# -*- coding: utf-8 -*-
import os
from datetime import datetime
from string import Template


def camel_to_underline(camel_format):
    underline_format = ''
    if isinstance(camel_format, str):
        for _s_ in camel_format:
            underline_format += _s_ if _s_.islower() else '_' + _s_.lower()
    return underline_format


def gen(app_id1, pkg_name1, key_word1, is_list, is_act):
    full_pkg = app_id1 + "." + pkg_name1
    #
    # gen_act(full_pkg)
    ui_type = "Act"
    if not is_act:
        ui_type = "Frgm"

    if is_act:
        if is_list:
            gen_list_act(ui_type, full_pkg, key_word1)
            gen_list_xml(ui_type.lower(), full_pkg, key_word1)
            gen_item_xml(ui_type.lower(), full_pkg, key_word1)
            gen_adapter("Adapter", full_pkg, key_word1)
            gen_presenter("Presenter", full_pkg, key_word1, ui_type)
            gen_model("Model", full_pkg, key_word1)
        else:
            gen_act(ui_type, full_pkg, key_word1)
            gen_single_xml(ui_type.lower(), full_pkg, key_word1)
            gen_presenter("Presenter", full_pkg, key_word1, ui_type, True)
            gen_model_single("Model", full_pkg, key_word1)
    else:
        if is_list:
            gen_list_frgm(ui_type, full_pkg, key_word1)
            gen_list_xml(ui_type.lower(), full_pkg, key_word1)
            gen_item_xml(ui_type.lower(), full_pkg, key_word1)
            gen_adapter("Adapter", full_pkg, key_word1)
            gen_presenter("Presenter", full_pkg, key_word1, ui_type)
            gen_model("Model", full_pkg, key_word1)
        else:
            gen_frgm(ui_type, full_pkg, key_word1)
            gen_single_xml(ui_type.lower(), full_pkg, key_word1)
            gen_presenter("Presenter", full_pkg, key_word1, ui_type, True)
            gen_model_single("Model", full_pkg, key_word1)


def gen_frgm(prefix, full_pkg, key_word1):
    # 模版文件
    class_name = key_word1 + prefix
    file_name = class_name + ".java"
    gen2(template_name="BaseFragment.java.tpl",
         package_name1=full_pkg,
         file_name2=file_name,
         KEY_WORD=key_word,
         PACKAGE_NAME=full_pkg,
         ANDROID_APP_ID=app_id,
         DATA=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
         CLASS_NAME=class_name,
         XML_NAME="frgm" + camel_to_underline(key_word))
    pass


def gen_list_act(prefix, full_pkg, key_word1):
    # 模版文件
    class_name = key_word1 + prefix
    file_name = class_name + ".java"
    gen2(template_name="BaseListActivity.java.tpl",
         package_name1=full_pkg,
         file_name2=file_name,
         KEY_WORD=key_word,
         PACKAGE_NAME=full_pkg,
         ANDROID_APP_ID=app_id,
         DATA=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
         CLASS_NAME=class_name,
         XML_NAME="act" + camel_to_underline(key_word),
         ITEM_XML_NAME="item" + camel_to_underline(key_word))
    pass


def gen_list_frgm(prefix, full_pkg, key_word1):
    # 模版文件
    class_name = key_word1 + prefix
    file_name = class_name + ".java"
    gen2(template_name="BaseListFragment.java.tpl",
         package_name1=full_pkg,
         file_name2=file_name,
         KEY_WORD=key_word,
         PACKAGE_NAME=full_pkg,
         ANDROID_APP_ID=app_id,
         DATA=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
         CLASS_NAME=class_name,
         XML_NAME="frgm" + camel_to_underline(key_word),
         ITEM_XML_NAME="item" + camel_to_underline(key_word))
    pass


def gen_act(prefix, full_pkg, key_word1):
    # 模版文件
    class_name = key_word1 + prefix
    file_name = class_name + ".java"
    gen2(template_name="BaseActivity.java.tpl",
         package_name1=full_pkg,
         file_name2=file_name,
         KEY_WORD=key_word,
         PACKAGE_NAME=full_pkg,
         ANDROID_APP_ID=app_id,
         DATA=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
         CLASS_NAME=class_name,
         XML_NAME="act" + camel_to_underline(key_word))
    pass


def gen_presenter(prefix, package_name1, key_word1, ui_type, is_single=False):
    # 模版文件
    class_name = key_word1 + prefix
    file_name = class_name + ".java"
    tmp_name = "BaseListPresenter"
    if is_single:
        tmp_name = "BasePresenter"
    gen2(template_name=tmp_name + ".java.tpl",
         package_name1=package_name1,
         file_name2=file_name,
         KEY_WORD=key_word,
         PACKAGE_NAME=package_name1,
         ANDROID_APP_ID=app_id,
         CLASS_NAME=class_name,
         UI_TYPE=ui_type,
         DATA=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
         )
    pass


def gen_adapter(prefix, package_name1, key_word1):
    # 模版文件
    class_name = key_word1 + prefix
    file_name = class_name + ".java"
    gen2(template_name="BaseAdapter.java.tpl",
         package_name1=package_name1,
         file_name2=file_name,
         KEY_WORD=key_word,
         PACKAGE_NAME=package_name1,
         ANDROID_APP_ID=app_id,
         CLASS_NAME=class_name,
         DATA=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
         )
    pass


def gen_model(prefix, package_name1, key_word1):
    class_name = key_word1 + prefix
    file_name = class_name + ".java"
    gen2(template_name="BaseListModel.java.tpl",
         package_name1=package_name1,
         file_name2=file_name,
         KEY_WORD=key_word,
         PACKAGE_NAME=package_name1,
         ANDROID_APP_ID=app_id,
         CLASS_NAME=class_name,
         DATA=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
         )
    pass


def gen_model_single(prefix, package_name1, key_word1):
    class_name = key_word1 + prefix
    file_name = class_name + ".java"
    gen2(template_name="BaseModel.java.tpl",
         package_name1=package_name1,
         file_name2=file_name,
         KEY_WORD=key_word,
         PACKAGE_NAME=package_name1,
         ANDROID_APP_ID=app_id,
         CLASS_NAME=class_name,
         DATA=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
         )
    pass


def gen_list_xml(prefix, package_name1, key_word1):
    # 模版文件
    file_name = prefix + camel_to_underline(key_word1) + ".xml"
    gen2(template_name="list.xml.tpl",
         package_name1=package_name1,
         file_name2=file_name)
    pass


def gen_item_xml(prefix, package_name1, key_word1):
    file_name = "item" + camel_to_underline(key_word1) + ".xml"
    gen2(template_name="item.xml.tpl",
         package_name1=package_name1,
         file_name2=file_name)
    pass


def gen_single_xml(prefix, package_name1, key_word1):
    # 模版文件
    file_name = prefix + camel_to_underline(key_word1) + ".xml"
    gen2(template_name="single.xml.tpl",
         package_name1=package_name1,
         file_name2=file_name)
    pass


def gen2(template_name, package_name1, file_name2, **kws):
    # 替换模板文件
    template_file = open(template_name, 'r')
    tmpl = Template(template_file.read())
    result = tmpl.substitute(kws)
    lines = []
    lines.append(result)

    # 0.将生成的代码写入文件 (包名和文件名 省去创建文件夹的麻烦)
    path = "./" + package_name1.replace('.', '/')
    file_path = r'%s/%s' % (path, file_name2)
    if os.path.exists(path) == False:
        os.makedirs(path)
    class_file = open(file_path, 'w')
    class_file.writelines(lines)
    class_file.close()


def gen_sp():
    key = "userToken"

    s1 = "public static boolean get() {"
    s1 = "\n\r"
    s1 = "return SpUtils.getInstance().getBoolean(ViSpCons.IS_FIRST, true);"
    s1 = "\n\r"
    s1 = "}"
    s1 = "\n\r"
    s1 = "public static void setFirst(boolean isFirstLogin) {"
    s1 = "\n\r"
    s1 = "SpUtils.getInstance().putBoolean(ViSpCons.IS_FIRST, isFirstLogin);"
    s1 = "\n\r"
    s1 = "}"
    s1 = "\n\r"
    pass


app_id = "com.woai.vichat"
pkg_name = "main"
key_word = "MyProfile"
gen(app_id, pkg_name, key_word, False, False)  # p1 是否列表 p2 是否Activity
